﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Utils;
using Obsidian.Edm;
using Obsidian.Data.Sql;
using Oak.Model;

namespace Oak.Base.Data
{
    public class AdminDA
    {

        internal BsAdminInfo SelectAdminByAdminName(string adminName)
        {
            BsAdminInfo a = new BsAdminInfo();
            IDbHandler dbh = a.CreateHandler();
            dbh.SetFields(a.Fields);
            dbh.Where(a.AdminName.Equals(adminName));
            DataReader dr = dbh.Select();
            return dr.ReadFirst<BsAdminInfo>(true);
        }

        internal BsAdminInfo SelectAdminByEmail(string email)
        {
            BsAdminInfo a = new BsAdminInfo();
            IDbHandler dbh = a.CreateHandler();
            dbh.SetFields(a.Fields);
            dbh.Where(a.Email.Equals(email));
            DataReader dr = dbh.Select();
            return dr.ReadFirst<BsAdminInfo>(true);
        }

        internal BsAdminInfo SelectAdminByMobileNo(string mobileNo)
        {
            BsAdminInfo a = new BsAdminInfo();
            IDbHandler dbh = a.CreateHandler();
            dbh.SetFields(a.Fields);
            dbh.Where(a.MobileNo.Equals(mobileNo));
            DataReader dr = dbh.Select();
            return dr.ReadFirst<BsAdminInfo>(true);
        }

        internal IList<BsAdminInfo> SelectAdminList(BsAdminQry qry, out ListAttrInfo listAttr)
        {
            BsAdminInfo a = new BsAdminInfo();
            IDbHandler dbh = a.CreateHandler();
            dbh.SetFields(a.Fields);

            if (!qry.Keyword.IsNullOrWhiteSpace)
            {
                CompoundCondition cc = new CompoundCondition();
                string keyword = qry.Keyword.TryTrim();
                cc.Or(a.AdminName.Like(keyword))
                    .Or(a.Email.Equals(keyword))
                    .Or(a.MobileNo.Equals(keyword));
                dbh.Where(cc);
            }

            if (!qry.Status.IsNull)
            {
                dbh.Where(a.Status.Equals(qry.Status.ToString()));
            }

            DataReader dr = dbh.Select(qry, out listAttr);
            List<BsAdminInfo> list = new List<BsAdminInfo>();
            while (dr.Read())
            {
                BsAdminInfo admin = new BsAdminInfo();
                dr.ReadTo(admin);
                list.Add(admin);
            }
            dr.Close();
            return list;
        }

    }
}
